<template>
	<view class="container">
		<view class="moments-list">
			<view class="moment-item" v-for="(item, index) in momentsList" :key="item.postId">
				<view class="moment-header">
					<image class="avatar" :src="'/static/vue-color-avatar.png'" mode="aspectFill"></image>
					<view class="user-info">
						<text class="nickname">{{item.nickname}}</text>
						<text class="time">{{item.timeAgo}}</text>
					</view>
				</view>
				<view class="moment-content">
					<text class="text-content">{{item.content}}</text>
					<view class="image-list" v-if="item.mediaUrls && item.mediaUrls.length">
						<image 
							v-for="(img, imgIndex) in item.mediaUrls" 
							:key="imgIndex" 
							:src="img" 
							mode="aspectFill"
							class="content-image"
							:class="{'single-image': item.mediaUrls.length === 1}"
						></image>
					</view>
					<text class="location" v-if="item.location">{{item.location}}</text>
				</view>
			</view>
		</view>
		<view class="loading" v-if="loading">加载中...</view>
		<view class="pagination">
			<button class="page-btn" :disabled="currentPage === 1" @click="loadPosts(currentPage - 1)">上一页</button>
			<text class="page-text">第 {{currentPage}} 页</text>
			<button class="page-btn" :disabled="noMore" @click="loadPosts(currentPage + 1)">下一页</button>
		</view>
	</view>
</template>

<script setup>
import { ref, onMounted } from 'vue'

const momentsList = ref([])
const currentPage = ref(1)
const pageSize = 10
const loading = ref(false)
const noMore = ref(false)

const loadPosts = (page = 1) => {
	if (loading.value) return
	
	loading.value = true
	
	uni.request({
		url: 'http://localhost:8080/api/posts/hot',
		method: 'GET',
		data: {
			page,
			size: pageSize
		},
		success: (res) => {
			if (!res.data) {
				console.error('响应数据异常:', res)
				uni.showToast({
					title: '加载失败',
					icon: 'none'
				})
				return
			}
			
			const { records, total } = res.data
			if (!records || !Array.isArray(records)) {
				console.error('数据格式错误:', res.data)
				uni.showToast({
					title: '数据格式错误',
					icon: 'none'
				})
				return
			}
			
			momentsList.value = records
			noMore.value = (page * pageSize) >= total
			currentPage.value = page
			
			if (records.length === 0) {
				uni.showToast({
					title: '暂无数据',
					icon: 'none'
				})
			}
		},
		fail: (error) => {
			console.error('加载失败:', error)
			uni.showToast({
				title: '加载失败，请检查网络连接',
				icon: 'none',
				duration: 2000
			})
			momentsList.value = []
		},
		complete: () => {
			loading.value = false
		}
	})
}

onMounted(() => {
	loadPosts(1)
})
</script>

<style>
.container {
	min-height: 100vh;
	padding: 20rpx;
	background-color: #f5f5f5;
	padding-bottom: 100rpx;
}

.moments-list {
	background-color: #fff;
	border-radius: 12rpx;
	overflow: hidden;
}

.moment-item {
	padding: 30rpx;
	border-bottom: 1rpx solid #f0f0f0;
}

.moment-header {
	display: flex;
	align-items: center;
	margin-bottom: 20rpx;
}

.avatar {
	width: 80rpx;
	height: 80rpx;
	border-radius: 50%;
	margin-right: 20rpx;
}

.user-info {
	flex: 1;
}

.nickname {
	font-size: 28rpx;
	color: #333;
	font-weight: bold;
	margin-bottom: 4rpx;
}

.time {
	font-size: 24rpx;
	color: #999;
}

.moment-content {
	margin-bottom: 20rpx;
}

.text-content {
	font-size: 28rpx;
	color: #333;
	line-height: 1.5;
	margin-bottom: 20rpx;
}

.image-list {
	display: grid;
	grid-template-columns: repeat(3, 1fr);
	gap: 10rpx;
}

.content-image {
	width: 100%;
	height: 200rpx;
	border-radius: 8rpx;
}

.single-image {
	width: 400rpx;
	height: 300rpx;
	grid-column: span 2;
}

.moment-footer {
	margin-top: 20rpx;
}

.action-list {
	display: flex;
	justify-content: flex-end;
	gap: 40rpx;
}

.action-item {
	display: flex;
	align-items: center;
	gap: 8rpx;
}

.iconfont {
	font-size: 32rpx;
	color: #999;
}

.icon-like.liked {
	color: #ff6b6b;
}

.action-text {
	font-size: 24rpx;
	color: #999;
}

.loading, .no-more {
	text-align: center;
	padding: 30rpx;
	color: #999;
	font-size: 26rpx;
	background: #fff;
	margin-top: 20rpx;
	border-radius: 12rpx;
}

.location {
	font-size: 24rpx;
	color: #666;
	margin-top: 10rpx;
}

.pagination {
	display: flex;
	justify-content: center;
	align-items: center;
	padding: 30rpx;
	background: #fff;
	margin-top: 20rpx;
	border-radius: 12rpx;
	gap: 20rpx;
}

.page-btn {
	font-size: 28rpx;
	padding: 10rpx 30rpx;
	background: #3cc51f;
	color: #fff;
	border-radius: 8rpx;
}

.page-btn[disabled] {
	background: #ccc;
	color: #fff;
}

.page-text {
	font-size: 28rpx;
	color: #666;
}
</style> 